<template>
    <d2-container>

        <h3>转出班级:</h3>
        {{originName}}
        <h3>请选择需要转入的班级:</h3>
        <el-radio-group v-model="wait_to_join_class">
            <el-radio-button v-for="_class in class_can_join" :label="_class._id" :key="_class._id">
                {{_class.name}}
            </el-radio-button>
        </el-radio-group>


        <h3>选择需要转入的学生</h3>
        <template>
            <el-transfer v-model="wait_join_student" :data="wait_leave_student">

            </el-transfer>
        </template>

        <h3 style="display: inline-block;">开始转入
            <el-button @click="translateStudent" type="primary" style="margin-left: 40px">
                转入新班级
            </el-button>
        </h3>

    </d2-container>
</template>

<script>
    import request from "@/plugin/axios";
    import {mapState} from "vuex";

    export default {
        data() {
            return {
                class_id: undefined,
                wait_join_student: [],
                wait_leave_student: [],
                wait_to_join_class: undefined,
                class_can_join: [],
                originName : ''
            }
        },
                computed: {
            ...mapState('d2admin/user', ['info']),
        },
        methods: {

            getProcurableClass() {
                let data ={}
                data.page = 1
                data.pageSize = 9999
                data.status = {$ne:2}
                data.school = this.school
                // data.course = this.class.course._id
                request({
                    url: "/class/getClassList",
                    method: "post",
                    data: data
                }).then(res => {
                    if (res.code == 200) {
                        this.class_can_join = res.data.class
                        let index = res.data.class.map(function(e){return e._id}).indexOf(this.class_id)
                        if (index != -1){


                        this.originName = res.data.class[index].name
                        this.class_can_join.splice(index,1)
                    }
                    }
                });
            },
            getClass(){
                request({
                    url: "/class/getClassOne",
                    method: "post",
                    data: {_id: this.class_id}
                }).then(res => {
                    if (res.code == 200) {
                        this.class = res.data
                         this.getClassStudent()
                    }
                });
            },

            getClassStudent() {
                request({
                    url: "/class/getClassStudentList",
                    method: "post",
                    data: {_id: this.class_id}
                }).then(res => {
                    if (res.code == 200) {
                        res.data.forEach((value, index) => {
                            this.wait_leave_student.push({key: value.student._id, label: value.student.name})
                        })
                         this.getProcurableClass()
                    }
                });
            },
            translateStudent() {
                console.log(this.wait_join_student,this.wait_leave_student)
                if (this.wait_to_join_class == undefined){
                    this.$alert('','请选择转入班级')
                    return false
                }
                if (this.wait_join_student.length == 0){
                    this.$alert('','请选择转入学生')
                    return false
                }
                request({
                    url: "/student/moveStudentsToOtherClass",
                    method: "post",
                    data: {
                        student: this.wait_join_student,
                        leave_class:this.class_id,
                        join_class:this.wait_to_join_class
                    }
                }).then(res => {
                    if (res.code == 200) {
                        this.$message({
                            type: "info",
                            message: "转入成功"
                        });
                       this.refresh = true
                        this.$router.back(-1);
                        
                    }
                });
            }
        },
        beforeRouteLeave(to,from,next){
    if (this.refresh){
      to.meta.refresh = 'a'
      this.refresh = undefined
    }
    next()
  },

        created() {
             this.school = this.info.school[0]._id
            if (this.$route.query.id != undefined) {
                this.class_id = this.$route.query.id
                this.getClass()
               
            }
        }

    };

</script>
